package com.robert.cool.weather.mvp.model;

import android.app.Application;

import com.google.gson.Gson;
import com.jess.arms.integration.IRepositoryManager;
import com.jess.arms.mvp.BaseModel;

import com.jess.arms.di.scope.ActivityScope;

import javax.inject.Inject;

import com.robert.cool.weather.mvp.contract.WeatherContract;
import com.robert.cool.weather.mvp.model.api.cache.CommonCache;
import com.robert.cool.weather.mvp.model.api.service.CommonService;
import com.robert.cool.weather.mvp.model.api.service.WeatherService;
import com.robert.cool.weather.mvp.model.entity.Weather;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.functions.Function;
import io.rx_cache2.DynamicKey;
import io.rx_cache2.EvictProvider;
import io.rx_cache2.Reply;
import okhttp3.ResponseBody;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 04/23/2020 13:02
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class WeatherModel extends BaseModel implements WeatherContract.Model {
    @Inject
    Gson mGson;
    @Inject
    Application mApplication;

    @Inject
    public WeatherModel(IRepositoryManager repositoryManager) {
        super(repositoryManager);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mGson = null;
        this.mApplication = null;
    }

    @Override
    public Observable<Weather> queryWeather(String url,String id,boolean refresh) {
        return Observable.just(
                mRepositoryManager.obtainRetrofitService(WeatherService.class).queryWeather(url)
        ).flatMap(new Function<Observable<Weather>, ObservableSource<Weather>>() {
            @Override
            public ObservableSource<Weather> apply(Observable<Weather> weatherObservable) throws Exception {
                return mRepositoryManager.obtainCacheService(CommonCache.class).queryWeather(weatherObservable,new DynamicKey(id),new EvictProvider(refresh))
                        .map(new Function<Reply<Weather>, Weather>() {
                            @Override
                            public Weather apply(Reply<Weather> weatherReply) throws Exception {
                                return weatherReply.getData();
                            }
                        });
            }
        });
    }

    @Override
    public Observable<ResponseBody> query(String url, String date, boolean refresh) {
        return  /*Observable.just(*/
                mRepositoryManager.obtainRetrofitService(CommonService.class).query(url)
        /*).flatMap(new Function<Observable<ResponseBody>, ObservableSource<ResponseBody>>() {
            @Override
            public ObservableSource<ResponseBody> apply(Observable<ResponseBody> data) throws Exception {
                return mRepositoryManager.obtainCacheService(CommonCache.class).query(data,new DynamicKey(date),new EvictProvider(refresh))
                        .map(new Function<Reply<ResponseBody>, ResponseBody>() {
                            @Override
                            public ResponseBody apply(Reply<ResponseBody> reply) throws Exception {
                                return reply.getData();
                            }
                        });
            }
        })*/;
    }
}